import java.util.Scanner;

class Student{


    public void fun() {
        System.out.println("fun!");
    }
}


public class Demo {

     static class Counter {
        volatile public int flag  = 0;
    }

    public static void main(String[] args) {

        Counter counter = new Counter();

        Thread t = new Thread(() -> {
            while(counter.flag == 0 ) {
                /////////////
            }

        });
        t.start();



        Thread t1 = new Thread(() -> {
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入一个数字:");
            int index = scan.nextInt();
            counter.flag = index;

        });

        t1.start();
    }
    //两个线程调用同一个锁对象 产生锁竞争  第一个拿到锁的执行完第二个线程才会执行
    public static void main1(String[] args) {
        Student student1 = new Student();
        Student student2 = new Student();

        Thread t = new Thread(() -> {

            synchronized(student1) {
                System.out.println("hello end");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("hello begin");
            }

        });
        t.start();

        Thread t1 = new Thread(() -> {

            synchronized(student1) {
                System.out.println("hello end");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("hello begin");
            }

        });
        t1.start();

    }


}
